package dashboard.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

import com.google.common.base.Splitter;
import com.google.common.collect.Maps;

import net.sf.json.JSONObject;

/**
 * @description 用于对trackurl进行解析的工具
 * @author zhaoxiang
 *
 */
public class TrackUrlUtils {
	public static final Splitter paramSplitter = Splitter.on('&').omitEmptyStrings();
	public static final Splitter jsonSplitter = Splitter.on("','").omitEmptyStrings();
	
	/**
	 * @description 格式化trackurl
	 * @param trackUrl
	 *            trackurl字符串
	 * @return 返回格式化后的字符串
	 */
	public static String formatTrackUrl(String trackUrl) {
		String tmpTrackUrl = null;
		if (StringUtils.isNotBlank(trackUrl)) {
			trackUrl = trackUrl.replace("\\x", "%").replace("%7B", "{")
					.replace("%7D", "}").replace("%7b", "{")
					.replace("%7d", "}").replace("%3A", ":")
					.replace("%3a", ":").replace("\"", "'").replace("%2C", ",")
					.replace("%2c", ",").replace(" ", "").replace("，", ",")
					.replace("｛", "{").replace("｝", "}").replace("：", ":")
					.replace("‘", "'").replace("’", "'").replace("%27", "'")
					.replace("%20", "").replace("':'", "\001")
					.replace("%22", "'").replace("','", "\002")
					.replace("':", "':'").replace(",'", "','")
					.replace("\001", "':'").replace("\002", "','")
					.replace(":'}", "''}").replace(",}", "}");
			int indexPre = trackUrl.indexOf("{");// 前缀
			if (indexPre != 0 && indexPre != -1)
				trackUrl = trackUrl.substring(indexPre, trackUrl.length());

			int indexSuf = trackUrl.lastIndexOf("}");
			if (indexSuf == -1) {
				int tmpIndex = trackUrl.lastIndexOf("':'");
				String prefix = trackUrl.substring(0, tmpIndex + 3);
				String suffix = trackUrl.substring(tmpIndex + 3,
						trackUrl.length());
				suffix = suffix.replace("'", "").replace(",", "")
						.replace("[", "");
				trackUrl = prefix + suffix + "'}";
			} else if (indexSuf != trackUrl.length() - 1) {
				trackUrl = trackUrl.substring(0, indexSuf + 1);
			}

			trackUrl = trackUrl.replace("':'", "\001").replace("','", "\002")
					.replace("'", "").replace("{", "").replace("}", "")
					.replace("\001", "':'").replace("\002", "','");

			int dIndex = trackUrl.lastIndexOf("','");
			int mIndex = trackUrl.lastIndexOf("':'");
			if (dIndex > mIndex)
				trackUrl = trackUrl.substring(0, dIndex);
			Iterable<String> trackUrls = jsonSplitter.split(trackUrl);
			if (trackUrls != null && trackUrl.length() != 0) {
				Map<String, String> map = new HashMap<String, String>();
				for (String tmp : trackUrls) {
					if (tmp.contains("':'")) {
						String[] tmps = tmp.split("':'", -1);
						String tmpKey = tmps[0];
						if (StringUtils.isNotBlank(tmpKey)) {
							tmpKey = tmpKey.replace("'", "");
							if (tmpKey.contains("%"))// 包含%再去解码，提高效率
								tmpKey = URLDecodeUtils.parser(tmpKey);

							if (StringUtils.isNotBlank(tmpKey)) {
								String tmpValue = tmps[1];
								String value =null;
								if (StringUtils.isNotBlank(tmpValue)) {
									tmpValue = tmpValue.replace("'", "");

									value=tmpValue;
									if (tmpValue.contains("%")){// 包含再去解码，提高效率
										value = URLDecodeUtils
												.parser(tmpValue);
	
										boolean tmpVF = URLDecodeUtils
												.regWord(tmpValue);
										if (!tmpVF)// 如果解码后出现乱码，则保留原样,无乱码则更新值
											value = URLDecodeUtils.getSearchEngines(tmpValue);
									}
								}
								map.put(tmpKey, value);
							}
						}
					}
				}

				if (map != null && map.size() != 0) {
					tmpTrackUrl = JSONObject.fromObject(map).toString();
				}
			}
		}
		return tmpTrackUrl;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String url="http://qt.qyer.com/beacon.gif/&category=place-mguide-view-picView-%5B1144905%5D&campaign=inpage&url=&referer=http%3A%2F%2Fplace.qyer.com%2Fmguide%2F6161%3FPGTID%3D4ec10a5a-8224-0743-97b3-82828012b4bf%26ClickID%3D2&referer_link=http%3A%2F%2Fwww.qyer.com%2F&guid=095486e6-8b7e-884f-9839-22ce9cc45e1a&user_id=7655612&session=1472528413689&timestamp=1472528477389&lat=&lng=&gpstimestamp=&ra_arg=&others=";
		System.err.println("url: "+url);
		System.err.println("URLDecodeUtils.parser(url): " + URLDecodeUtils.parser(url));
		System.err.println( "URLDecoder.decode(url, utf-8): " + URLDecoder.decode(url, "utf-8"));
		List<NameValuePair> paramList = URLEncodedUtils.parse(url, Charset.forName("UTF-8"));
		for(NameValuePair pair : paramList){
			System.out.println(pair);
		}
	}
}
